package com.hethongviet.job.pages;

import com.hethongviet.job.AppMain;
import com.hethongviet.job.R;
import com.hethongviet.job.helpers.CodeResult;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ChangePasswordActivity extends Activity
{
	private Button _buttonAgree = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.page_change_pass);
		
		final EditText passCurrentEditText = (EditText) findViewById(R.id.textAccountPasswordCurrent);
		final EditText passNewEditText = (EditText) findViewById(R.id.textAccountPasswordNew);
		final EditText passNewReInputEditText = (EditText) findViewById(R.id.textAccountPasswordNewReinput);

		_buttonAgree = (Button) findViewById(R.id.buttonChangePassAgree);
		_buttonAgree.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				final String passCurrent = passCurrentEditText.getText().toString();
				if (passCurrent.equals(""))
				{
					AppMain.showAlert(ChangePasswordActivity.this,
							R.string.textErrorMessageAccountPassEmpty, false);
					return;
				}
				if (!passCurrent.equals(AppMain._user.getPassword()))
				{
					AppMain.showAlert(ChangePasswordActivity.this,
							R.string.textErrorMessageAccountPassWrong, false);
					passCurrentEditText.setText("");
					return;
				}
				final String passNew = passNewEditText.getText().toString();
				if (passNew.equals(""))
				{
					AppMain.showAlert(ChangePasswordActivity.this,
							R.string.textErrorMessageAccountPassNewEmpty, false);
					return;
				}
				final String passNewReinput = passNewReInputEditText.getText().toString();
				if (passNewReinput.equals(""))
				{
					AppMain.showAlert(
							ChangePasswordActivity.this,
							R.string.textErrorMessageAccountPassNewReinputEmpty,
							false);
					return;
				}
				if (!passNewReinput.equals(passNew))
				{
					AppMain.showAlert(ChangePasswordActivity.this,
							R.string.textErrorMessageAccountPassNewNotMatch,
							false);
					return;
				}

				showProgressBar();
				new Thread(new Runnable() {
					public void run()
					{
						AppMain._codeResultService = AppMain.doChangePass(
								AppMain._user.getUsername(), passCurrent,
								passNew);
						AppMain.hideProgressDialog();
					}
				}).start();
			}
		});
	}

	@Override
	protected void onDestroy()
	{
		AppMain.hideAllDialog();
		super.onDestroy();
	}
	
	private void showProgressBar()
	{
		AppMain.showProgress(this);
		enableAllClick(false);
	}

	private void enableAllClick(boolean enable)
	{
		_buttonAgree.setClickable(enable);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus)
	{
		AppMain.debug("Window focus change!");
		if (AppMain._isDoneLoading)
		{
			switch (AppMain._codeResultService)
			{
				case ERROR_NETWORK:
					AppMain.showAlert(this, R.string.textErrorNetwork, false);
					break;
				case SUCCESS_CHANGE_PASS:
					AppMain.showAlert(this, R.string.textPassworkResultRight,
							true);
					break;
				case FAIL_CHANGE_PASS:
					AppMain.showAlert(this,
							R.string.textErrorMessageAccountPassWrong, false);
					break;
				default:
					break;
			}
			AppMain._codeResultService = CodeResult.NONE;
			AppMain._isDoneLoading = false;
			enableAllClick(true);
		}
		super.onWindowFocusChanged(hasFocus);
	}
}
